package org.example.src.controller;

import org.example.src.dto.response.ApiResponse;
import org.example.src.entity.Company;
import org.example.src.entity.User;
import org.example.src.service.UserService;
import org.example.src.service.StudentService;
import org.example.src.service.TeacherService;
import org.example.src.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/admin")
public class AdminController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CompanyService companyService;

    /**
     * 获取所有用户
     */
    @GetMapping("/users")
    public ResponseEntity<ApiResponse<List<User>>> getAllUsers() {
        List<User> users = userService.findAllUsers();
        return success("获取用户列表成功", users);
    }

    /**
     * 根据角色获取用户
     */
    @GetMapping("/users/role/{role}")
    public ResponseEntity<ApiResponse<List<User>>> getUsersByRole(@PathVariable String role) {
        List<User> users = userService.findUsersByRole(role);
        return success("获取用户列表成功", users);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/users/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return success("用户删除成功",null);
    }

    /**
     * 更新用户角色
     */
    @PutMapping("/users/{id}/role")
    public ResponseEntity<ApiResponse<User>> updateUserRole(@PathVariable Long id, @RequestParam String role) {
        User user = userService.findUserById(id);
        if (user == null) {
            return notFound("用户未找到");
        }
        user.setRole(role);
        User updatedUser = userService.updateUser(user);
        return success("用户角色更新成功", updatedUser);
    }

    /**
     * 获取系统统计信息
     */
    @GetMapping("/dashboard")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getDashboardStats() {
        Map<String, Object> stats = new HashMap<>();

        stats.put("totalUsers", userService.findAllUsers().size());
        stats.put("totalStudents", studentService.findAllStudents().size());
        stats.put("totalTeachers", teacherService.findAllTeachers().size());
        stats.put("totalCompanies", companyService.findAllCompanies().size());

        return success("获取统计信息成功", stats);
    }

    /**
     * 审核企业
     */
    @PutMapping("/companies/{id}/approve")
    public ResponseEntity<ApiResponse<Void>> approveCompany(@PathVariable Long id) {
        try {
            companyService.approveCompany(id);
            return success("企业审核通过",null);
        } catch (Exception e) {
            return error("企业审核失败: " + e.getMessage());
        }
    }

    /**
     * 获取待审核企业列表
     */
    @GetMapping("/companies/pending")
    public ResponseEntity<ApiResponse<List<Company>>> getPendingCompanies() {
        List<Company> companies = companyService.findCompaniesByApprovalStatus(false);
        return success("获取待审核企业列表成功", companies);
    }
}
